package com.chat.api.web.restctrl;

import com.chat.api.utils.ResultUtil;
import com.chat.api.utils.util.ParamUtil;
import com.chat.api.web.po.RemarkBean;
import com.chat.api.web.service.RemarkService;
import com.chat.api.web.vo.MessageEntity;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/remark")
public class RemarkCtrl {

    @Autowired
    private RemarkService remarkService;

    @RequestMapping("list")
    @ApiOperation("获取评论列表")
    public List list(ModelMap model,@RequestParam(value = "bId",required = false) String bId ,
                     HttpServletRequest request) {
        String idStr = ParamUtil.getParameter(request, "id", "");
        Long id = ParamUtil.getLongParameter(idStr, -1L);
        Long blogId = ParamUtil.getLongParameter(bId, -1L);
        List<List> list = remarkService.getList(id,blogId,request);
        return list;
    }

    @RequestMapping("getBlogRemarkCount")
    @ResponseBody
    @ApiOperation("获取博客下的评论数量")
    public MessageEntity getBlogRemarkCount(ModelMap model, HttpServletRequest request) {
        return remarkService.getBlogRemarkCount();
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ApiOperation("保存并发表评论")
    public MessageEntity save(@RequestParam(value = "content") String content,
                              @RequestParam(value = "rrId") String rrId,
                              @RequestParam(value = "blogId") String blogId,HttpServletRequest request) {
        HttpSession session = request.getSession();
        String idStr = (String) session.getAttribute(session.getId());
        Long userId = ParamUtil.getLongParameter(idStr, -1L);
        RemarkBean remarkBean = new RemarkBean();
        remarkBean.setRrId(ParamUtil.getLongParameter(rrId,-1));
        remarkBean.setContent(content);
        Long blogIdL = ParamUtil.getLongParameter(blogId, 0L);
        remarkBean.setBlogId(blogIdL);
        String result = remarkService.save(userId,remarkBean);
        if (result.equals("ok")) {
            return ResultUtil.success("成功");
        }else {
            return ResultUtil.fail(result);
        }
    }

}
